package cn.tom.service.impl;

import cn.tom.entity.Information;
import cn.tom.mapper.InformationMapper;
import cn.tom.service.InformationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service    //实例化装配到IOC容器
@CacheConfig //缓存配置
public class InformationServiceImpl implements InformationService {
    @Autowired
    InformationMapper informationMapper;      //注入接口InformationMapper

    /**
     * Logger输出
     */
    private Logger log
            = LoggerFactory.getLogger(cn.tom.tool.Log.class);

    /**
     * 查询全部资讯
     */
    @Override
    public List<Information> findAll() {
        List<Information> lst = informationMapper.selectList(null);
        return lst;
    }

    /**
     * 根据资讯id查询
     */

    @Override
    public Information findById(Integer id) {
        Information information = informationMapper.findById(id);
        return information;
    }

    /**
     * 更新资讯浏览量+1
     */
    @Override
    public int update_page_view(Information information) {
        information.setPage_view(information.getPage_view() + 1);
        int i = informationMapper.updateById(information);
        return i;
    }

    /**
     * 添加一个资讯
     */
    @Override
    public int add(Information information) {
        information.setDay(new Date());     //赋值当前时间
        int insert = informationMapper.insert(information);
        return insert;
    }

    /**
     * 修改一个资讯
     */
    @Override
    public int update(Information information) {
        int update = informationMapper.updateById(information);
        return update;
    }

    /**
     * 删除一个资讯
     */
    @Override
    public int del(Integer id) {
        int del = informationMapper.deleteById(id);
        return del;
    }

    /**
     * 分页查询
     */
    @Override
    public List<Information> findByPage(Integer start, Integer num, Integer typeId) {
        Map<String, Object> map = new HashMap<>();
        map.put("typeId", typeId);
        map.put("start", start);
        map.put("num", num);
        return informationMapper.findByPage(map);
    }

    /**
     * 根据时间查询最新的6条资讯
     */
    @Override
    public List<Information> findByDay() {
        List<Information> findByDay = informationMapper.findByDay();
        return findByDay;
    }

    /**
     * 根据类型ID查询总数
     */
    @Override
    public int findCount(Integer typeId) {
        int count = informationMapper.findCount(typeId);
        return count;
    }
}
